package config

import (
	"fmt"
	"github.com/urfave/cli"
	"gopkg.in/yaml.v2"
	"os"
)

type File struct {
	Dir        string `yaml:"dir" json:"dir"`
	BaseUrl    string `yaml:"base_url" json:"base_url"`
	User       string `yaml:"user"`
	Password   string `yaml:"password"`
	ServerPort string `yaml:"server_port"`
}
type Config struct {
	File File `yaml:"file" json:"file"`
}

var config Config
var initConfig = false

func GetConfig() Config {
	if initConfig {
		return config
	} else {
		panic("config file not init......")
	}

}
func InitConfig(ctx *cli.Context) (Config, error) {
	cfg := ctx.String("cfg")
	if cfg == "" {
		cfg = "./config.yaml"
	}
	if initConfig {
		return config, nil
	}
	fmt.Println("加载配置文件:", cfg)
	yml, err := os.ReadFile(cfg)
	if err != nil {
		return config, err
	}

	err = yaml.Unmarshal(yml, &config)
	if err != nil {
		return config, err
	}
	initConfig = true
	return config, nil
}
